package cn.tedu.v6;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * Version 4  客户端  【登录】  退出
 */

public class Client {
    private static final String HOST = "192.168.226.187";
    private static final Integer PORT = 1357;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;
    public Client() {
        try {
            socket = new Socket(HOST, PORT);
            ip = socket.getInetAddress().getHostAddress();
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
        } catch(SocketException e){
            System.out.println();
        } catch (UnknownHostException e) {
            System.out.println("IP地址有问题，请询问服务器端！");
        } catch (IOException e) {
            System.out.println("客户端主动退出了：断开链接");
        }
    }

    public void start() {
            System.out.println("~~~~欢迎使用XX牌QQ聊天室~~~~");
            Scanner input=new Scanner(System.in);
            //目的：不捕获，确定没有问题，捕获代码块具体针对服务器结构
            try {
                //一、接收登录提示
                //1.读入服务器--输出流
                String serverMessage = br.readLine();//服务器端:写“请输入用户名”
                System.out.println(serverMessage);//读入账号，必须写出
                //2.写出用户名
                String username = input.nextLine();//输入用户名
                //3.写出用户名，发给服务器
                pw.println(username);//写出给服务器端---获取账号

                //二、接收登陆密码提示
                //1.读入客户端----输出流
                serverMessage = br.readLine(); //服务器端：写“请输入密码：”
                System.out.println(serverMessage); //打印：请输入密码：
                //2.写出密码
                String password = input.nextLine();
                //3.写出密码，发给服务器
                pw.println(password);
                //三、服务器端验证账号和密码是否正确
                //3.1读入账号和密码正确的情况
                serverMessage=br.readLine();//登陆成功，进入XX聊天室
                System.out.println(serverMessage);//客户端打印：登录成功...
                //3.2 判断如果是”登陆成功,进入XX聊天室“  前缀：登陆成功
                if(serverMessage.startsWith("登陆成功")){ //true  聊天
                    chat();//聊天功能
                }
                if(serverMessage.equalsIgnoreCase("exit")){
                    System.exit(0);
                }

            }catch(IOException e){
                System.out.println("客户端断开连接~");
            }finally{
            if (pw != null)
                pw.close();
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯内容失败~~");
                }
            if (socket != null && !socket.isClosed()) {
                System.out.println("客户端关闭成功！success~");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            }
        }

//读
       /* while(true){
         String str=br.readLine();
            System.out.println(""+str);
        }*/
    }
    //1.start方法下--封装    客户端   相互通话话方法  chat
private void chat() {
    new Thread(() -> {
        try {
            String message;
            while ((message = br.readLine()) != null) {
                System.out.println("服务器说：" + message);
                System.out.println("服务器请输入：");

            }
        } catch(UnsupportedEncodingException e){
            System.out.println();
        }catch(SocketException e) {
            System.out.println();
        }catch (IOException e) {
            System.out.println("服务器端发来的消息断开了~");
        }
    }).start();

    Scanner input = new Scanner(System.in);
    while (true) {
        System.out.println("客户端请输入:");
        String say = input.nextLine();
        pw.println("客户端说：" + say);
        if(say.equalsIgnoreCase("exit")){
            System.exit(0);
        }


    }
}
    public static void main(String[] args) {
        new Client().start();
    }
}

